package com.uinfor.controller;

import com.uinfor.entity.Payment;
import com.uinfor.entity.Result;
import com.uinfor.service.IPaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @descripttion:
 * @Author: WZH
 * @Date :2021/1/18  9:54
 **/
@RestController
@Slf4j
public class PaymentController {
    @Autowired
    private IPaymentService paymentService;
    @Value("${server.port}")
    private String serverPort;
    @PostMapping(value = "/payment/create")
    public Result create(@RequestBody Payment payment){
        log.info(payment.getSerial());
        int i = paymentService.create(payment);
        log.info("****************插入结果"+i+serverPort);
        Result<Object> result = new Result<>();
        if (i>0){
            result.setCode(200);
            result.setMessage("插入成功"+serverPort);
        }else {
            result.setCode(444);
        }
        return result;
    }
    @GetMapping(value = "/payment/get/{id}")
    public Result getPaymentById(@PathVariable("id")Long id){
        Payment paymentById = paymentService.getPaymentById(id);
        log.info("****************查询结果是"+paymentById.toString()+serverPort);
        Result<Object> result = new Result<>();
        if (!"".equals(paymentById.getId())){
            result.setCode(200);
            result.setMessage("查询成功"+serverPort);
            result.setData(paymentById);
        }else {
            result.setCode(444);
            result.setMessage("查询失败");
        }
        return result;
    }
    @GetMapping("/payment/lb")
    public String getPaymentLb(){
        return serverPort;
    }


//    @ResponseBody
//    @GetMapping("/hello")
//    public String hello(){
//        return "nihao";
//    }
@GetMapping("/payment/timeout")
public String payMentFeignTimeOut(){
    try{
        TimeUnit.SECONDS.sleep(3);
    }catch (Exception e){
        e.printStackTrace();
    }
    return serverPort;
}
}
